package collections3;
import java.io.*;
import java.util.*;


public class StudentManage {

	
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		
		try {
			HashMap<String, Student> map = new HashMap<String, Student>();
			File file = new File("d:/1.txt");
			Scanner input = new Scanner(System.in);
			ObjectOutputStream oos = null;
			ObjectInputStream ois = null;

			while (true) {
				System.out.println("1.显示学生信息\n2.增加学生信息\n3.修改学生信息\n4.删除学生信息\n9.退出");
				System.out.println("请输入你的选择：");
				String choice = input.next();
				switch (choice) {
				case "1":
					System.out.println("***显示学生信息***");
					if (file.length() > 0) {
						ois = new ObjectInputStream(new FileInputStream(file));
						map = (HashMap<String, Student>) ois.readObject();
						Set<String> set = map.keySet();
						for (Iterator<String> it = set.iterator(); it.hasNext();) {
							String o = it.next();
							System.out.println(o + ":" + map.get(o));
						}
						ois.close();
					} else {
						System.out.println("没有学生！");
					}				
					break;
				case "2":
					System.out.println("***增加学生信息***");
					System.out.println("请输入学生姓名：");
					String name = input.next();
					System.out.println("请输入学生身份证：");
					String identification = input.next();
					System.out.println("请输入学生学号：");
					String numberStudent = input.next();
					System.out.println("请输入学生年龄：");
					int age = input.nextInt();
					Student student = new Student(name, numberStudent,
							identification, age);
					oos = new ObjectOutputStream(new FileOutputStream(file));
					if (map.containsKey(numberStudent)) {
						System.out.println("该学号已存在，请选择是否替换value值？（y/其他字符）");
						String choice2 = input.next();
						if (choice2.equals("y")) {
							map.put(numberStudent, student);
						}
					} else {
						map.put(numberStudent, student);
					}
					oos.writeObject(map);
					oos.close();
					break;
				case "3":
					System.out.println("***修改学生信息***");
					System.out.println("请输入需要修改的学生姓名：");
					String amendName = input.next();
					System.out.println("请输入学生身份证：");
					String amendIdentification = input.next();
					System.out.println("请输入学生学号：");
					String amendNumberStudent = input.next();
					System.out.println("请输入学生年龄：");
					int amendAge = input.nextInt();
					Student amendStudent = new Student(amendName,
							amendNumberStudent, amendIdentification, amendAge);
					oos = new ObjectOutputStream(new FileOutputStream(file));
					if (map.containsKey(amendNumberStudent)) {
						map.put(amendNumberStudent, amendStudent);
					} else {
						System.out.println("没有该学号！");
					}
					oos.writeObject(map);
					oos.close();
					break;
				case "4":
					System.out.println("***删除学生信息***");
					System.out.println("请输入学生学号：");
					String inputnumberStudent = input.next();
					oos = new ObjectOutputStream(new FileOutputStream(file));
					if (map.containsKey(inputnumberStudent)) {
						map.remove(inputnumberStudent);
						System.out.println("删除成功！");
					} else {
						System.out.println("没有该学号！");
					}
					oos.writeObject(map);
					oos.close();
					break;
				case "9":
					System.out.println("***退出***");
					return;
				}
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}
}